<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Creation of Measurements - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.measure.creation.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.measure.creation.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.measure.introduction.html">Introduction</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.measure.html">Zend_Measure</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.measure.output.html">Outputting measurements</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.measure.creation" class="section"><div class="info"><h1 class="title">Creation of Measurements</h1></div>
    

    <p class="para">
        When creating a measurement object, <span class="classname">Zend_Measure_*</span> methods expect the
        input/original measurement data value as the first parameter. This can be a
        <a href="zend.measure.creation.html#zend.measure.creation.number" class="link">numeric argument</a>, a
        <a href="zend.measure.creation.html#zend.measure.creation.string" class="link"><span class="type">String</span></a> without units, or a
        <a href="zend.measure.creation.html#zend.measure.creation.localized" class="link">localized string with unit(s)
            specified.</a> The second parameter defines the type of the measurement. Both
        parameters are mandatory. The language may optionally be specified as the third parameter.
    </p>

    <div class="section" id="zend.measure.creation.number"><div class="info"><h1 class="title">Creating measurements from integers and floats</h1></div>
        

        <p class="para">
            In addition to integer data values, floating point types may be used, but
            <a href="http://www.php.net/float" class="link external">&raquo; &quot;simple decimal fractions like 0.1 or 0.7 cannot
                be converted into their internal binary counterparts without a little loss of
                precision,&quot;</a> sometimes giving surprising results. Also, do not compare two
            &quot;float&quot; type numbers for equality.
        </p>

        <div class="example" id="zend.measure.creation.number.example-1"><div class="info"><p><b>Example #1 Creation using integer and floating values</b></p></div>
            

            <pre class="programlisting brush: php">
$measurement = 1234.7;
$unit = new Zend_Measure_Length((integer)$measurement,
                                Zend_Measure_Length::STANDARD);
echo $unit;
// outputs &#039;1234 m&#039; (meters)

$unit = new Zend_Measure_Length($measurement, Zend_Measure_Length::STANDARD);
echo $unit;
// outputs &#039;1234.7 m&#039; (meters)
</pre>

        </div>
    </div>

    <div class="section" id="zend.measure.creation.string"><div class="info"><h1 class="title">Creating measurements from strings</h1></div>
        

        <p class="para">
            Many measurements received as input to Zend Framework applications can only be passed
            to <span class="classname">Zend_Measure_*</span> classes as strings, such as numbers written
            using <a href="http://en.wikipedia.org/wiki/Roman_numerals" class="link external">&raquo; roman numerals</a>
            or extremely large binary values that exceed the precision of <acronym class="acronym">PHP</acronym>&#039;s
            native integer and float types. Since integers can be denoted using strings, if there is
            any risk of losing precision due to limitations of <acronym class="acronym">PHP</acronym>&#039;s native
            integer and float types, using strings instead.
            <span class="classname">Zend_Measure_Number</span> uses the BCMath extension to support
            arbitrary precision, as shown in the example below, to avoid limitations in many
            <acronym class="acronym">PHP</acronym> functions, such as <a href="http://php.net/bin2dec" class="link external">&raquo;  <span class="methodname">bin2dec()</span></a>.
        </p>

        <div class="example" id="zend.measure.creation.string.example-1"><div class="info"><p><b>Example #2 Creation using strings</b></p></div>
            

            <pre class="programlisting brush: php">
$mystring = &quot;10010100111010111010100001011011101010001&quot;;
$unit = new Zend_Measure_Number($mystring, Zend_Measure_Number::BINARY);

echo $unit;
</pre>

        </div>
    </div>

    <div class="section" id="zend.measure.creation.localized"><div class="info"><h1 class="title">Measurements from localized strings</h1></div>
        

        <p class="para">
            When a string is entered in a localized notation, the correct interpretation can not be
            determined without knowing the intended locale. The division of decimal digits with &quot;.&quot;
            and grouping of thousands with &quot;,&quot; is common in the English language, but not so in
            other languages. For example, the English number &quot;1,234.50&quot; would be interpreted as
            meaning &quot;1.2345&quot; in German. To deal with such problems, the locale-aware
            <span class="classname">Zend_Measure_*</span> family of classes offer the possibility to specify
            a language or region to disambiguate the input data and properly interpret the intended
            semantic value.
        </p>

        <div class="example" id="zend.measure.creation.localized.example-1"><div class="info"><p><b>Example #3 Localized string</b></p></div>
            

            <pre class="programlisting brush: php">
$locale = new Zend_Locale(&#039;de&#039;);
$mystring = &quot;1,234.50&quot;;
$unit = new Zend_Measure_Length($mystring,
                                Zend_Measure_Length::STANDARD,
                                $locale);
echo $unit; // outputs &quot;1.234 m&quot;

$mystring = &quot;1,234.50&quot;;
$unit = new Zend_Measure_Length($mystring,
                                Zend_Measure_Length::STANDARD,
                                &#039;en_US&#039;);
echo $unit; // outputs &quot;1234.50 m&quot;
</pre>

        </div>

        <p class="para">
            Since Zend Framework 1.7.0 <span class="classname">Zend_Measure</span> does also support the
            usage of an application wide locale. You can simply set a
            <span class="classname">Zend_Locale</span> instance to the registry like shown below. With this
            notation you can forget about setting the locale manually with each instance when you
            want to use the same locale multiple times.
        </p>

        <pre class="programlisting brush: php">
// in your bootstrap file
$locale = new Zend_Locale(&#039;de_AT&#039;);
Zend_Registry::set(&#039;Zend_Locale&#039;, $locale);

// somewhere in your application
$length = new Zend_Measure_Length(Zend_Measure_Length::METER();
</pre>

    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.measure.introduction.html">Introduction</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.measure.html">Zend_Measure</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.measure.output.html">Outputting measurements</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="reference.html">Zend Framework Reference</a></li>
  <li class="header up"><a href="zend.measure.html">Zend_Measure</a></li>
  <li><a href="zend.measure.introduction.html">Introduction</a></li>
  <li class="active"><a href="zend.measure.creation.html">Creation of Measurements</a></li>
  <li><a href="zend.measure.output.html">Outputting measurements</a></li>
  <li><a href="zend.measure.edit.html">Manipulating Measurements</a></li>
  <li><a href="zend.measure.types.html">Types of measurements</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>